﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Freeform;

namespace Space_in_Shaders
{
    public class EnemyBomber : SpaceShip
    {

        public Single Thrust { get; set; }
        public Single TurnTorque { get; set; }
        public Single FireAngThreshold { get; set; }
        public Single FireDispThreshold { get; set; }
        public Boolean LeftThrust { get; set; }
        public Boolean UpThrust { get; set; }
        public Boolean DownThrust { get; set; }
        public Boolean RightThrust { get; set; }
        public Int32 TurnState { get; set; }

        public EnemyBomber(SpaceWorld w)
            :base (w)
        {
            Game.LoadAnimation(this);
            Actor.Depth = 0.2f;
            SpatialProperties.Size = new Vector2(12, 18);
            NMot.TorqueResistance = 0.995f;
            PuffSpacing = 200;
            NextPuff = PuffSpacing;
            Thrust = 30f;
            TurnTorque = 1f;
            MissileSpacing = 1350;
            MissileSpeed = 180f;
            LaserOffset = 16;
            NextMissile = MissileSpacing;
            FireAngThreshold = 0.4f;
            FireDispThreshold = 32000f;
            CollisionProperties.CollisionID = RadialCollision.CID_AISHIP;
            Radial.Radius = 8;
            CollisionProperties.SetCollisionMask(RadialCollision.CID_LASER, true);
            CollisionProperties.SetCollisionMask(RadialCollision.CID_MISSILE, true);
            CollisionProperties.SetCollisionMask(RadialCollision.CID_AISHIP, true);
            CollisionProperties.SetCollisionMask(RadialCollision.CID_PLAYERSHIP, true);
            Health = 13;
            ExplosionIntensity = 5;
            Points = 2;

            ImageActor.Animations.AnimationFinished += delegate(AnimationSet S, Animation A)
            {
                if (ImageActor.Animations.Current.AnimationName == "Firing")
                {
                    ImageActor.Animations.PlayAnimation("Normal");
                    Int32 ReqLoop = (Int32) (MissileSpacing * ImageActor.Animations.Current.FrameRate / ImageActor.Animations.Current.Frames / 1000);
                    ImageActor.Animations.Current.Loop = ReqLoop;
                }
                else
                {
                    ImageActor.Animations.PlayAnimation("Normal");
                    ImageActor.Animations.Current.Loop = 0;
                }
            };
        }

        public override void Update(GameTime time)
        {

            base.Update(time);

            // Set the direction of ship facing the mouse pointer.
            Vector2 Disp = this.SpaceWorld.Player.SpatialProperties.Position - SpatialProperties.Position;
            Single ReqAng = (Single)Math.Atan2(Disp.X, -Disp.Y);
            Single Diff = ReqAng - SpatialProperties.Direction;
            if (Diff > Math.PI)
                Diff -= MathHelper.TwoPi;
            if (Diff < -Math.PI)
                Diff += MathHelper.TwoPi;

            TurnState = Diff > 0 ? 1 : -1;

            if (Math.Abs(Diff) < FireAngThreshold && Disp.LengthSquared() < FireDispThreshold)
                IsFiringMissile = true;
            else
                IsFiringMissile = false;
            
            // This is a hokey method for playing the firing anim once per missile launch.
            if (IsFiringMissile && ImageActor.Animations.Current.Loop == 0)
            {
                ImageActor.Animations.PlayAnimation("Firing");
                ImageActor.Animations.Current.Loop = 1;
            }

            NMot.ApplyTorque(TurnTorque * TurnState);

            IsThrusting = true;

            if (IsThrusting)
                NMot.ApplyForce(Util.Vec2InDir(SpatialProperties.Direction) * Thrust);
        }
    }
}
